package com.tripupdater.view
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Bounce;
	import com.tripupdater.model.Params;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class PasswordView extends MovieClip
	{
		private var txtPassword:TextField;
		private var aceptar:MovieClip;
		private var cancelar:MovieClip;
		
		public function PasswordView()
		{
			super();
			
			createChildren();
		}
		
		private function createChildren():void
		{
			txtPassword= getChildByName("txtPasswordInstance") as TextField;
			aceptar= getChildByName("aceptarInstance") as MovieClip;
			cancelar= getChildByName("cancelarInstance") as MovieClip;
			
			txtPassword.displayAsPassword= true;
			
			aceptar.useHandCursor= true;
			aceptar.buttonMode= true;
			aceptar.mouseChildren= false;
			
			cancelar.useHandCursor= true;
			cancelar.buttonMode= true;
			cancelar.mouseChildren= false;
			
			aceptar.addEventListener(MouseEvent.CLICK, mouseHandler);
			cancelar.addEventListener(MouseEvent.CLICK, mouseHandler);
		}
		
		private function mouseHandler(e:MouseEvent):void
		{
			if(e.target == aceptar){
				if(txtPassword.text.toLowerCase() == Params.PRIVATE_PASS){
					dispatchEvent(new Event(Event.ACTIVATE));
					clear();
				} else {
					clear();
				}
			} else
			if(e.target == cancelar){
				clear();
			}
		}
		
		private function clear():void
		{
			txtPassword.text= "";
			TweenLite.to(this, 0.5, {scaleX: 0, scaleY: 0, easing: Bounce.easeInOut, onComplete: function(){ this.visible= false; }});
		}
	}
}